from datetime import datetime

from django.db import models
from django.utils.translation import ugettext_lazy as _
from imagekit.models import ImageSpecField
from pilkit.processors import ResizeToFill, TrimBorderColor, Adjust, SmartResize

from accounts.models import UserProfile
from django_ueditor.models import UEditorField
from utils.resize_image import Watermark


def get_image_path(model_instance=None):
    if model_instance is None:
        return "images/ueditor/"
    else:
        return "images/ueditor/%s/" % model_instance.Name


class Outline(models.Model):
    """
        单元
    """
    name = models.CharField(verbose_name=_('name'), max_length=255)
    sort_order = models.IntegerField(verbose_name=_('sort_order'))
    add_time = models.DateTimeField(verbose_name=_('add_time'), default=datetime.now)
    description = models.CharField(verbose_name=_('description'), max_length=255)

    class Meta:
        verbose_name = _('outline')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Secene(models.Model):
    """
    单元里的场景
    """
    title = models.CharField(verbose_name=_('title'), max_length=255)
    outline = models.ForeignKey(Outline, verbose_name=_('outline'), null=False, blank=False)
    image_first = models.ImageField(max_length=100, upload_to='images/%Y/%m', null=False, blank=False,
                                    verbose_name=_('secene_image_first'))
    image_second = models.ImageField(max_length=100, upload_to='images/%Y/%m', null=True, blank=True,
                                     verbose_name=_('secene_image_second'), default=None)
    add_time = models.DateTimeField(verbose_name=_('add_time'), default=datetime.now)
    description = UEditorField(verbose_name=_('description'), default='',
                               imagePath=get_image_path(), filePath='files/', toolbars="mini")
    type = models.CharField(max_length=22, choices=(('find_stubble', _('find_stubble')),
                                                    ('secene_desc', _('secene_desc'))),
                            default='secene_desc',
                            verbose_name=_('type'))

    class Meta:
        verbose_name = _('secene')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Comment(models.Model):
    """
    场景里的评论
    """
    content = models.TextField(verbose_name=_('content'), default='', null=False, blank=False)
    add_time = models.DateTimeField(verbose_name=_('add_time'), default=datetime.now)
    secene = models.ForeignKey(Secene, verbose_name=_('secene'))
    user = models.ForeignKey(UserProfile, verbose_name=_('relation_user'))

    class Meta:
        verbose_name = _('comment')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class SeceneDescType(models.Model):
    """
    场景描述的类型
    """
    name = models.CharField(verbose_name=_('name'), max_length=40)
    description = models.CharField(verbose_name=_('description'), max_length=255)

    class Meta:
        verbose_name = _('desc_type')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class SeceneMarkDesc(models.Model):
    """
    场景里的描述的点
    """
    content = models.TextField(verbose_name=_('desc_content'))
    add_time = models.DateTimeField(verbose_name=_('add_time'), default=datetime.now)
    secene = models.ForeignKey(Secene, verbose_name=_('relation_secene'))
    user = models.ForeignKey(UserProfile, verbose_name=_('relation_user'))
    type = models.ForeignKey(SeceneDescType, verbose_name=_('desc_type'))
    image_num = models.IntegerField(verbose_name=_('which_image'), default=1)

    class Meta:
        verbose_name = _('secene_mark_desc')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class SeceneGroup(models.Model):
    name = models.CharField(verbose_name="情景的组名", max_length=100)

    class Meta:
        verbose_name = _('情景组')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
